using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 敌人设置
/// </summary>
public class EnemyController : MonoBehaviour
{
    [Header("敌人物理移动参数")]
    [Tooltip("设置速度")]
    public float speed = 3f;
    
    [Header("敌人")]
    private Rigidbody2D rb;
    
    [Header("爆炸")]
    public GameObject explosionEffect;
    
    [Header("玩家")]
    private Transform player;
    
    /// <summary>
    /// 获取玩家和敌人组件
    /// </summary>
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        player = GameObject.FindGameObjectWithTag("Player").transform;
    }
    
    /// <summary>
    /// 敌人移动设置
    /// </summary>
    void FixedUpdate()
    {
        if (player != null)
        {
            Vector2 direction = (player.position - transform.position).normalized;
            rb.velocity = direction * speed;;
        }
    }

    /// <summary>
    /// 敌人相关爆炸音效、粒子和销毁设置
    /// </summary>
    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.CompareTag("Bullet"))
        {
            GameManager.Instance.AddScore(10);
            GameManager.Instance.PlayExplosionSound();
            
            Destroy(other.gameObject);//销毁子弹
            Destroy(gameObject);//销毁自己
            //生成爆炸效果
            GameObject instance = Instantiate(explosionEffect, transform.position, Quaternion.identity);
            Destroy(instance,0.5f);
        }
    }
    
}
